<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php 
	if( !isset($_COOKIE["username"]) ){
		echo "<script>window.location.href='login.php'</script>";
	}else{
		setcookie("username", $_COOKIE["username"],time() + 900);
	}
?>
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>List Doctor</title>
<link href="css/template.css" rel="stylesheet" type="text/css" />
<script src="js/droplinemenu.js" type="text/javascript"></script>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
//build menu with DIV ID="myslidemenu" on page:
droplinemenu.buildmenu("nav")
</script>

<link href="css/ui-lightness/jquery-ui-1.9.2.custom.css" rel="stylesheet">
<script src="js/jquery-1.8.3.js"></script>
<script src="js/jquery-ui-1.9.2.custom.js"></script>


</head>

<body>
	<div id="container">
		<div id="header"></div>
		<?php include_once 'nav.php';?>
		<?php 
			$ini_array = parse_ini_file("config/config.ini");
			$conn = @mysqli_connect($ini_array["host"], $ini_array["username"], $ini_array["password"], $ini_array["db_name"]) or die("cann't connect the mysql db");
			mysqli_set_charset($conn, "utf8");
			ini_set('default_charset', "UTF-8");
			$getClinicSql = "select Clinic_no, Clinic_name from clinic";
			$clinicResult = mysqli_query($conn, $getClinicSql);
		?>
		
		<?php 

			
			
			
			$getDoctorSql = "select Doctor_no, doctor_first_name, doctor_surname, doctor_address, Clinic_name from doctor left outer join clinic on doctor.clinic_no = clinic.Clinic_no ";
			
			$hasParameter = false;
			$doctorNoFormatError = false;
			$clinicNoFormatError = false;
			if( isset($_POST["doctorNo"]) && $_POST["doctorNo"] != ""){
				
				if( preg_match("/^\d+$/", $_POST["doctorNo"]) == true){
					if( $hasParameter == false ){
						$getDoctorSql .= " where ";
					}else{
						$getDoctorSql .= " and ";
					}
					$getDoctorSql .= " Doctor_no=".$_POST["doctorNo"]." ";
					$hasParameter = true;
				}else{
					$doctorNoFormatError = true;
				}
			}
			
			if( isset($_POST["firstName"]) && $_POST["firstName"] != ""){
				if( $hasParameter == false ){
					$getDoctorSql .= " where ";
				}else{
					$getDoctorSql .= " and ";
				}
				$getDoctorSql .= " doctor_first_name like '%".$_POST["firstName"]."%' ";
				$hasParameter = true;
			}
			
			if( isset($_POST["surname"]) && $_POST["surname"] != ""){
				if( $hasParameter == false ){
					$getDoctorSql .= " where ";
				}else{
					$getDoctorSql .= " and ";
				}
				$getDoctorSql .= " doctor_surname like '%".$_POST["surname"]."%' ";
				$hasParameter = true;
			}
			
			if( isset($_POST["clinic"]) && $_POST["clinic"] != 0 ){
				if( preg_match("/^\d+$/", $_POST["clinic"]) == true){
					if( $hasParameter == false ){
						$getDoctorSql .= " where ";
					}else{
						$getDoctorSql .= " and ";
					}
					$getDoctorSql .= " doctor.clinic_no=".$_POST["clinic"];
					$hasParameter = true;
				}else{
					$clinicNoFormatError = true;
				}
			}
			$doctorResult = mysqli_query($conn, $getDoctorSql);
			
		?>
		<div id="search">
			<form id="searchForm" name="searchForm" method="post" action="list_doctor.php">
				<table style="width:1190px">
					<tr>
					<td><label>Doctor No.:</label><input type="text" name="doctorNo" id="doctorNo" value="<?php if( isset($_POST["doctorNo"]))echo $_POST["doctorNo"]; ?>" style="width:100px;"/></td>
					<td><label>Doctor First Name:</label><input type="text" name="firstName" id="firstName" value="<?php if( isset($_POST["firstName"])) echo $_POST["firstName"]?>" style="width:100px;"/> </td>
					<td><label>Doctor Surname:</label><input type="text" name="surname" id="surname" value="<?php if( isset($_POST["surname"])) echo $_POST["surname"]?>" style="width:100px;"/> </td>
					<td>
						<label>Clinic:</label>
						<select name="clinic" id="clinic" style="width:180px;">
							<?php 
								$clinicNo = 0;
								if( isset($_POST["clinic"]) ){
									$clinicNo = $_POST["clinic"];
								}
							?>
							<option value="0" <?php if( $clinicNo == 0 ) echo "selected=\"selected\"";?>></option>
							<?php
								while($row = mysqli_fetch_array($clinicResult) ){
							?>
							<option value="<?php echo $row["Clinic_no"]?>" <?php if( $clinicNo == $row["Clinic_no"]) echo "selected=\"selected\"";?>><?php echo $row["Clinic_name"]?></option>
							<?php 
								}
							?>
						</select>
					</td>
					<td>
						<input type="submit" name="query" value="Search"/>
						<input type="button" name="new_doctor" value="New_Doctor" onclick="window.location.href='doctor_info.php'"/>
						<label><?php if( $doctorNoFormatError == true || $clinicNoFormatError == true ) echo "doctor or clinic No. error!";?></label>
					</td>
					</tr>
				</table>
				
			</form>
		</div>
		<div id="main_content" style="text-align:center">
			<div><h2>List of Doctors</h2></div>
			<div style="width:135px;display:inline-block;"><label>Doctor No.</label></div>
			<div style="width:135px;display:inline-block;"><label>First Name</label></div>
			<div style="width:135px;display:inline-block;"><label>Surname</label></div>
			<div style="width:180px;display:inline-block;"><label>Address</label></div>
			<div style="width:135px;display:inline-block;"><label>Clinic</label></div>
			<div style="width:135px;display:inline-block;"><label>Operate</label><br /></div>
			
			
			
			
			
			<div id="doctorList" style="margin:0px auto;width:840px;height:520px;overflow:auto;">
				<table style="width:820px">
					<?php 
						while( $row = mysqli_fetch_array($doctorResult)){
							$doctorNo = $row["Doctor_no"];
							$doctorFirstName = $row["doctor_first_name"];
							if( strlen($doctorFirstName) > 15 ){
								$doctorFirstName = substr($doctorFirstName, 0, 15);
							}
							
							$doctorSurname = $row["doctor_surname"];
							if( strlen($doctorSurname) > 15 ){
								$doctorSurname = substr($doctorSurname, 0, 15);
							}

							
							$doctorAddress = $row["doctor_address"];
							if( strlen($doctorAddress) > 25 ){
								$doctorAddress = substr($doctorAddress, 0, 36);
							}
							
							$clinicName = $row["Clinic_name"];
							if( strlen($clinicName) > 15 ){
								$clinicName = substr($clinicName, 0, 15);
							} 
							
					?>
					<tr>
						<td style="width:10%;"><?php echo $doctorNo?></td>
						<td style="width:18%;"><?php echo $doctorFirstName?></td>
						<td style="width:18%;"><?php echo $doctorSurname?></td>
						<td style="width:26%;"><?php echo $doctorAddress?></td>
						<td style="width:18%;"><?php echo $clinicName?></td>
						<td style="width:10%;"><a href="doctor_info.php?doctorNo=<?php echo $doctorNo?>">view</a></td>
					</tr>
					<?php }?>
				</table>
			</div>
			<input type="button" value="return" onclick="window.location.href='index.php'"/>
		</div>
		<?php include_once 'footer.php';?>
	</div>
</body>
</html>
